﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebDS;
using WebUS;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using System.Data;
using IP.Core.IPData.DBNames;
using IP.Core.IPData;
using IP.Core.IPUserService;

public partial class ChucNang_F201_Quan_ly_phan_hoi_tu_benh_vien : System.Web.UI.Page
{
    US_DM_DOT_BAO_TRI m_us_dm_dot_bao_tri = new US_DM_DOT_BAO_TRI();
    DS_DM_DOT_BAO_TRI m_ds_dm_dot_bao_tri = new DS_DM_DOT_BAO_TRI();
    protected void Page_Load(object sender, EventArgs e)
    {

        load_data_2_gridview();
        if (!IsPostBack)
        {
            load_data_2_cbo_ten_ben_a();
            load_data_2_cbo_dai_dien_ben_b();
        }
    }

    private void load_data_2_us_object(int stt_row_in_gridview)
    {
        try
        {
            decimal id_phan_hoi_benh_vien = CIPConvert.ToDecimal(m_grv_dm_danh_gia_tu_benh_vien.DataKeys[stt_row_in_gridview].Value);
            m_us_dm_dot_bao_tri = new US_DM_DOT_BAO_TRI(id_phan_hoi_benh_vien);
            decimal test = m_us_dm_dot_bao_tri.dcID_BEN_A;
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }        
    }

    private void form_2_us_object()
    {
        m_us_dm_dot_bao_tri.dcID_BEN_A = CIPConvert.ToDecimal(m_cbo_ten_benh_vien.SelectedValue);
        m_us_dm_dot_bao_tri.datNGAY_BAO_TRI=m_txt_ngay_bao_tri.SelectedDate;
        m_us_dm_dot_bao_tri.strNOI_THUC_HIEN_BAO_TRI=m_txt_noi_thuc_hien_bao_tri.Text;
        m_us_dm_dot_bao_tri.datTHOI_GIAN_HOAN_TAT=m_txt_thoi_gian_hoan_tat.SelectedDate;
        m_us_dm_dot_bao_tri.strDAI_DIEN_BEN_A=m_txt_dai_dien_ben_a.Text;
        m_us_dm_dot_bao_tri.strCHU_VU_DAI_DIEN_BEN_A=m_txt_chuc_vu_dai_dien_ben_a.Text;
        m_us_dm_dot_bao_tri.dcID_DAI_DIEN_BEN_B=CIPConvert.ToDecimal(m_cbo_dai_dien_ben_b.SelectedValue);
        m_us_dm_dot_bao_tri.strDANH_GIA_CUA_BV_VOI_NHAN_VIEN=m_txt_danh_gia_benh_vien.Text;
    }

    private void us_object_2_form()
    {
        m_cbo_ten_benh_vien.SelectedValue = m_us_dm_dot_bao_tri.dcID_BEN_A.ToString();
        m_txt_ngay_bao_tri.SelectedDate = m_us_dm_dot_bao_tri.datNGAY_BAO_TRI;
        m_txt_noi_thuc_hien_bao_tri.Text = m_us_dm_dot_bao_tri.strNOI_THUC_HIEN_BAO_TRI;
        m_txt_thoi_gian_hoan_tat.SelectedDate = m_us_dm_dot_bao_tri.datTHOI_GIAN_HOAN_TAT;
        m_txt_dai_dien_ben_a.Text = m_us_dm_dot_bao_tri.strDAI_DIEN_BEN_A;
        m_txt_chuc_vu_dai_dien_ben_a.Text = m_us_dm_dot_bao_tri.strCHU_VU_DAI_DIEN_BEN_A;
        US_DM_NHAN_VIEN v_us_dm_nhan_vien = new US_DM_NHAN_VIEN(m_us_dm_dot_bao_tri.dcID_DAI_DIEN_BEN_B);
        m_cbo_dai_dien_ben_b.SelectedValue = v_us_dm_nhan_vien.dcID.ToString();
        m_txt_danh_gia_benh_vien.Text = m_us_dm_dot_bao_tri.strDANH_GIA_CUA_BV_VOI_NHAN_VIEN;
    }

    private void load_data_2_gridview()
    {
        try
        {
            m_us_dm_dot_bao_tri.FillDataset(m_ds_dm_dot_bao_tri);
            m_grv_dm_danh_gia_tu_benh_vien.DataSource = m_ds_dm_dot_bao_tri;
            m_grv_dm_danh_gia_tu_benh_vien.DataBind();
        }
        catch (Exception v_e)
        {
            
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void load_data_2_cbo_ten_ben_a()
    {
        try
        {
            US_DM_DIA_PHUONG_BENH_VIEN v_us_ten_dia_phuong_benh_vien = new US_DM_DIA_PHUONG_BENH_VIEN();
            DS_DM_DIA_PHUONG_BENH_VIEN v_ds_ten_dia_phuong_benh_vien = new DS_DM_DIA_PHUONG_BENH_VIEN();
            v_us_ten_dia_phuong_benh_vien.FillDataset(v_ds_ten_dia_phuong_benh_vien);
            m_cbo_ten_benh_vien.DataSource = v_ds_ten_dia_phuong_benh_vien.DM_DIA_PHUONG_BENH_VIEN;
            m_cbo_ten_benh_vien.DataTextField = DM_DIA_PHUONG_BENH_VIEN.TEN_DIA_PHUONG;
            m_cbo_ten_benh_vien.DataValueField = DM_DIA_PHUONG_BENH_VIEN.ID;
            m_cbo_ten_benh_vien.DataBind();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void load_data_2_cbo_dai_dien_ben_b()
    {
        try
        {
            US_DM_NHAN_VIEN v_us_dm_nhan_vien = new US_DM_NHAN_VIEN();
            DS_DM_NHAN_VIEN v_ds_dm_nhan_vien = new DS_DM_NHAN_VIEN();
            v_us_dm_nhan_vien.FillDataset(v_ds_dm_nhan_vien);
            m_cbo_dai_dien_ben_b.DataSource = v_ds_dm_nhan_vien.DM_NHAN_VIEN;
            m_cbo_dai_dien_ben_b.DataTextField = DM_NHAN_VIEN.HO_VA_TEN_DEM;
            m_cbo_dai_dien_ben_b.DataValueField = DM_NHAN_VIEN.ID;
            m_cbo_dai_dien_ben_b.DataBind();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_danh_gia_tu_benh_vien_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        try
        {
            load_data_2_us_object(e.RowIndex);
            us_object_2_form();
            m_hdf_check_chon.Value = m_us_dm_dot_bao_tri.dcID.ToString();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
        
    }
    protected void m_grv_dm_danh_gia_tu_benh_vien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_us_dm_dot_bao_tri = new US_DM_DOT_BAO_TRI(CIPConvert.ToDecimal(m_grv_dm_danh_gia_tu_benh_vien.DataKeys[e.RowIndex].Value));
            m_us_dm_dot_bao_tri.Delete();            
            m_lbl_xoa.Text = "Xóa thành công dữ liệu";
            load_data_2_gridview();
        }
        catch (Exception v_e)
        {
            m_lbl_xoa.Text = "Xóa không thành công";
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_btn_them_moi_Click(object sender, EventArgs e)
    {
        try
        {
            form_2_us_object();
            m_us_dm_dot_bao_tri.Insert();
            load_data_2_gridview();
            m_lbl_them_moi.Text = "Thêm mới thành công";
        }
        catch (Exception v_e)
        {
            m_lbl_them_moi.Text = "Thêm mới thất bại";
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_btn_cap_nhat_Click(object sender, EventArgs e)
    {
        if(m_hdf_check_chon.Value == "")
        {
            m_lbl_cap_nhat.Text = "Bạn chưa chọn dòng dữ liệu cần cập nhật!";
        }else
        {
            try
            {
                form_2_us_object();
                m_us_dm_dot_bao_tri.dcID = CIPConvert.ToDecimal(m_hdf_check_chon.Value);
                m_us_dm_dot_bao_tri.Update();
                load_data_2_gridview();
                m_lbl_cap_nhat.Text = "Cập nhật thành công";
            }
            catch (Exception v_e)
            {
                m_lbl_cap_nhat.Text = "Cập nhật thất bại";
                CSystemLog_301.ExceptionHandle(this, v_e);
            }
        }
        
    }
    protected void m_btn_xoa_trang_Click(object sender, EventArgs e)
    {
        m_cbo_ten_benh_vien.SelectedIndex = 0;
        m_txt_noi_thuc_hien_bao_tri.Text = "";
        m_txt_dai_dien_ben_a.Text = "";
        m_txt_chuc_vu_dai_dien_ben_a.Text = "";
        m_cbo_dai_dien_ben_b.SelectedIndex = 0;
        m_txt_danh_gia_benh_vien.Text = "";
    }
}